<?php

namespace app\api\controller\Ordertaker;

//use AlibabaCloud\Client\Request\Request;
use app\admin\model\Evaluate;
use app\admin\model\user\Address;
use app\api\model\JoinMerchant;
use app\api\model\User;
use app\api\services\CacheServices;
use app\api\services\store\OrderServices;
use app\api\validate\SubmitOrderValidate;
use app\common\controller\Api;
use think\Db;
use think\Request;
use think\Queue;
use app\api\model\JoinOnboarding;


class Order extends Api
{

    protected $noNeedRight = '*';
    protected $order_taker_id;
    private  $user_token = '';

    private  $array_action=['rider_order_list'];
    public function _initialize(Request $request = null)
    {
//        parent::__construct($request);
//        var_dump();die;

        //用户登录token
        $action=Request::instance()->action();

        $token=Request::instance()->header('token');
        $this->user_token=json_decode(base64_decode($token),1);
        $this->order_taker_id=JoinOnboarding::where(['user_id'=>$this->user_token['user_id']])->value('id');
        if(!in_array($action,$this->array_action)){

            if(!$this->user_token){

                $this->success("未传token");

            }else{
                
                $this->order_taker_id=JoinOnboarding::where(['user_id'=>$this->user_token['user_id']])->value('id');
                
            }
        }
//
    }



//    /**
//     * 商家订单搜索
//     * store/order/store_search
//     * @param Request $request
//     * @return \think\response\Json
//     * @throws \think\db\exception\DataNotFoundException
//     * @throws \think\db\exception\ModelNotFoundException
//     * @throws \think\exception\DbException
//     */
//    public  function  store_search(Request $request){
//        $post=$request->post();
//
//         $where=[];
//         if($post['search']){
//             $where['a.order_sn']=['like','%'.$post['search'].'%'];
//
//         }
//        $where['a.store_id']=$this->store_id;
//        $store=\app\api\model\Order::where($where)
//            ->alias('a')
//            ->join('user_address b','a.address_id=b.id')
//            ->field('a.id,a.order_sn,a.order_status,a.actual_price,a.createtime,b.real_name,b.phone,b.detail,a.address_id')
//            ->page($post['page'],$post['pagesize'])
//            ->order('createtime desc')
//            ->select();
//
//        return json(['code'=>200,'msg'=>'请求成功','data'=>$store]);
//    }


    /**
     * 骑手订单列表
     * Ordertaker/order/rider_order_list
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function  rider_order_list(Request $request){
        $post=$request->post();

        $where=[];
//        $where['product_type']=$post['product_type']+1;
//        if($post['status']>0){
//            $where['status']=$post['status']-1;
//        }
        //拖车
        if($post['product_type']==0){
            $where['product_type']=4;
            if($post['status']==0){
                $where['rider_status']=0;
            }
            if($post['status']==1){
                $where['rider_status']=['in','1,2,3'];
            }
            if($post['status']==2){
                $where['rider_status']=4;
            }
            $store=\app\api\model\Order::where($where)
                ->field('id,actual_price,trailer_address,trailer_longitude,trailer_latitude,user_location,user_longitude,user_latitude,consignee,mobile,rider_status')
                ->page($post['page'],$post['pagesize'])
                ->order('createtime desc')
                ->select();
            foreach ($store as  &$value){

                $a=getDistance($post['lat'],$post['lng'],$value['trailer_latitude'],$value['trailer_longitude']);
                $b=getDistance($post['lat'],$post['lng'],$value['user_latitude'],$value['user_longitude']);
                $value['km']=$a+$b.'km';
            }
        }
        //换电池
        if($post['product_type']==2){
            $where['store_id']=$this->order_taker_id;
            if($post['status']==0){
                $where['p_status']=0;
            }
            if($post['status']==3){
                $where['p_status']=7;
            }
            $store=\app\api\model\Order::where($where)
                ->field('id,actual_price,trailer_address,store_name,trailer_longitude,trailer_latitude,user_location,user_longitude,user_latitude,consignee,mobile,rider_status')
                ->page($post['page'],$post['pagesize'])
                ->order('createtime desc')
                ->select();
            foreach ($store as  &$value){
                $a=getDistance($post['lat'],$post['lng'],$value['trailer_latitude'],$value['trailer_longitude']);
                $b=getDistance($post['lat'],$post['lng'],$value['user_latitude'],$value['user_longitude']);
                $value['km']=$a+$b.'km';
            }
        }
        //服务
        if($post['product_type']==1){
            $where['product_type']=5;
            if($post['status']==0){
                $where['rider_status']=0;
            }
            if($post['status']==1){
                $where['rider_status']=['in','1,2,3'];
            }
            if($post['status']==2){
                $where['rider_status']=4;
            }
            $store=\app\api\model\Order::where($where)
//                ->alias('a')
//                ->join('user_address b','a.address_id=b.id')
                ->field('id,actual_price,servicetype,user_location,user_longitude,user_latitude,consignee,mobile,rider_status,service_j')
                ->page($post['page'],$post['pagesize'])
                ->order('createtime desc')
                ->select();
            foreach ($store as &$value){
                $value['estimated_delivery_time']='00:00';

            }
        }
        //开锁
        if($post['product_type']==3){
            $where['product_type']=6;
            if($post['status']==0){
                $where['rider_status']=0;
            }
            if($post['status']==1){
                $where['rider_status']=['in','1,2,3'];
            }
            if($post['status']==2){
                $where['rider_status']=4;
            }
            $store=\app\api\model\Order::where($where)
                ->field('id,actual_price,unlock_type,user_location,user_longitude,user_latitude,consignee,mobile,rider_status')
                ->page($post['page'],$post['pagesize'])
                ->order('createtime desc')
                ->select();
            foreach ($store as &$value){
                $value['estimated_delivery_time']='00:00';
                $b=getDistance($post['lat'],$post['lng'],$value['user_latitude'],$value['user_longitude']);
                $value['km']=$b.'km';
            }
        }

        return json(['code'=>200,'msg'=>'请求成功','data'=>$store]);
    }

    /**
     * Ordertaker/order/mall_orders
     * 拖车顶订单详情
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function mall_orders(Request $request)
    {
      $post=$request->post();
        $store=json_array(\app\api\model\Order::where(['id'=>$post['order_id']])
              ->find());

        $data['actual_price']=$store['actual_price'];
        $data['expected_arrival']='00:00';
        $data['rider_status']=$store['rider_status'];
        $data['aftersale_status']=$store['aftersale_status'];
        $data['q_appeal_status']=$store['q_appeal_status'];
        $data['vehicle']=[
              'text'=>'车',
              'trailer_address'=>$store['trailer_address'],
              'trailer_longitude'=>$store['trailer_longitude'],
              'trailer_latitude'=>$store['trailer_latitude'],
              'km'=>getDistance($post['lat'],$post['lng'],$store['trailer_latitude'],$store['trailer_longitude'])
        ];

        $data['Destination']=[
            'text'=>'目',
            'user_location'=>$store['user_location'],
            'user_longitude'=>$store['user_longitude'],
            'user_latitude'=>$store['user_latitude'],
            'km'=>getDistance($post['lat'],$post['lng'],$store['user_latitude'],$store['user_longitude']),
            'consignee'=>$store['consignee'],
            'mobile'=>$store['mobile'],
        ];
        //现场照片
        $data['on_site_photos']=$store['on_site_photos']??yuming(explode(',',$store['on_site_photos']),'');
        //故障车型
        $data['defective_vehicle_model']=$store['defective_vehicle_model'];
        $data['time']=[
            'createtime'=>date('Y-m-d H:i:s',$store['createtime']),
            'order_acceptance_time'=>$store['order_acceptance_time']??date('Y-m-d H:i:s',$store['order_acceptance_time']),
            'arrival_time_of_vehicle'=>$store['arrival_time_of_vehicle']??date('Y-m-d H:i:s',$store['arrival_time_of_vehicle']),
            'arrival_time_at_destination'=>$store['arrival_time_at_destination']??date('Y-m-d H:i:s',$store['arrival_time_at_destination']),
        ];
        $data['refund']=[
            //退款原因
            'refund_content'=>$store['refund_content'],
            //退款时间
            'refund_apply_time'=>$store['refund_apply_time'],
            //退款描述
            'describe_credentials'=>$store['describe_credentials'],
        ];

        $data['appeal']=[
            //申诉时间
            'appeal_time'=>$store['appeal_time'],
            //申诉原因
            'reason_for_appeal'=>$store['reason_for_appeal'],
            //申述描述
            'statement_description'=>$store['statement_description'],
            //申述图片
            'picture_of_appeal'=>$store['picture_of_appeal'],
        ];
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
    }


    /**
     * 拖车骑手点击接单
     * Ordertaker/order/taking_orders
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function taking_orders(Request $request)
    {
        $post=$request->post();
        $rider_status=\app\api\model\Order::where(['id'=>$post['order_id']])->value('rider_status');
        if($rider_status==1){
            return  json(['code'=>404,'msg'=>'订单已被接取，请选择其他的订单','data'=>'']);
        }
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>1,'order_acceptance_time'=>time(),'q_user_id'=>$this->order_taker_id]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已接单','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'接单失败，请稍后再试','data'=>'']);
        }
    }
    /**
     * 拖车已到达车辆位置
     * Ordertaker/order/refusal_to_refund
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function vehicle_position_has_been_reached(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>2,'arrival_time_of_vehicle'=>time()]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已到达车辆位置','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'确认状态失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 拖车已到达目的地
     * Ordertaker/order/arrived_at_destination
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function arrived_at_destination(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>3,'arrival_time_at_destination'=>time()]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已到达车辆位置','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'确认状态失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 拖车删除订单
     * Ordertaker/order/delete_order
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function delete_order(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->delete();
        if($Order){
            return  json(['code'=>200,'msg'=>'订单已删除','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'删除失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 拖车接单员申诉
     * Ordertaker/order/complaint_from_the_order_taker
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function complaint_from_the_order_taker(Request $request)
    {
        $post=$request->post();
        $data=[
             'appeal_time'=>time(),
             'reason_for_appeal'=>$post['reason_for_appeal'],
             'statement_description'=>$post['statement_description'],
             'picture_of_appeal'=>$post['picture_of_appeal'],
             'q_appeal_status'=>1,

        ];
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update($data);
        if($Order){
            return  json(['code'=>200,'msg'=>'已申诉','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'申诉失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 电池订单详情
     * Ordertaker/order/battery_orders
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     *
     */
    public  function battery_orders(Request $request)
    {

        $post=$request->post();
        $store=json_array(\app\api\model\Order::where(['id'=>$post['order_id']])
            ->find());
//        var_dump($store);die;
        $data['specifications']=json_encode($store['specifications'],1);
        $data['vehicle']=[
            'text'=>'取',
            'trailer_address'=>$store['trailer_address'],
            'trailer_longitude'=>$store['trailer_longitude'],
            'store_name'=>$store['store_name'],
            'trailer_latitude'=>$store['trailer_latitude'],
            'km'=>getDistance($post['lat'],$post['lng'],$store['trailer_latitude'],$store['trailer_longitude'])
        ];
        $data['Destination']=[
            'text'=>'收',
            'user_location'=>$store['user_location'],
            'user_longitude'=>$store['user_longitude'],
            'user_latitude'=>$store['user_latitude'],
            'km'=>getDistance($post['lat'],$post['lng'],$store['user_latitude'],$store['user_longitude']),
            'consignee'=>$store['consignee'],
            'mobile'=>$store['mobile'],
            'is_door'=>$store['is_door']
        ];
        //是否上门安装
        $data['is_door']=$store['is_door']>0?'是':'否';

        $data['order_status']=$store['order_status'];
        $data['aftersale_status']=$store['aftersale_status'];
        $data['money']=[
            //押金
            'deposit'=>$store['deposit'],
            //商品总费用
            'goods_price'=>$store['goods_price'],
            //优惠金额
            'coupon_price'=>$store['coupon_price'],
            //总金额
            'order_price'=>$store['order_price'],
        ];
        $data['time']=[
            //取货单号
            'pickup_note_number'=>$store['pickup_note_number'],
            //订单号
            'order_sn'=>$store['order_sn'],
            //下单时间
            'createtime'=>date('Y-m-d H:i:s',$store['createtime']),
            //接单时间
            'order_acceptance_time'=>$store['order_acceptance_time']??date('Y-m-d H:i:s',$store['order_acceptance_time']),
            //是否上门
            'is_door'=>$store['is_door']>0?'是':'否',
            //安装费
            'installation_fee'=>$store['installation_fee'],
            //配送费
            'delivery_fee'=>$store['delivery_fee'],
            //取货时间
            'pickup_time'=>$store['pickup_time']??date('Y-m-d H:i:s',$store['pickup_time']),
            //送达时间
            'deliveryd_time'=>$store['deliveryd_time']??date('Y-m-d H:i:s',$store['deliveryd_time']),
            //服务完成时间
            'service_completion_time'=>$store['service_completion_time']??date('Y-m-d H:i:s',$store['service_completion_time']),
        ];

        $data['refund']=[
            'refund_content'=>$store['refund_content'],
            'after_sales_type'=>$store['after_sales_type'],
            'refund_amount'=>$store['refund_amount'],
            'refund_type'=>$store['refund_type'],
            'refund_apply_time'=>$store['refund_apply_time'],
            'refund_time'=>$store['refund_time'],
            'describe_credentials'=>$store['describe_credentials'],
        ];
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);

    }

    /**
     * 换电池接单订单
     * Ordertaker/order/replacing_the_battery_order
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function replacing_the_battery_order(Request $request)
    {
        $post=$request->post();
        $rider_status=\app\api\model\Order::where(['id'=>$post['order_id']])->value('p_status');
        if($rider_status==2){
            return  json(['code'=>404,'msg'=>'订单已被接取，请选择其他的订单','data'=>'']);
        }

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['q_user_id'=>$this->order_taker_id,'order_acceptance_time'=>time(),'p_status'=>2]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已接单','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'接单失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 换电池确认取货订单
     * Ordertaker/order/confirm_pickup_order
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function confirm_pickup_order(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['pickup_time'=>time(),'p_status'=>3]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已取货','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'取货失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 换电池确认完成订单
     * Ordertaker/order/confirm_delivery_order
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function confirm_delivery_order(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['deliveryd_time'=>time(),'p_status'=>4]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已送达','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'送达失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 换电池删除订单
     * Ordertaker/order/replace_battery_and_delete
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function replace_battery_and_delete(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->delete();
        if($Order){
            return  json(['code'=>200,'msg'=>'已删除','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'删除失败，请稍后再试','data'=>'']);
        }
    }


    /**
     * 换电池接单员申诉
     * Ordertaker/order/appeal_for_battery_replacement
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function appeal_for_battery_replacement(Request $request)
    {
        $post=$request->post();
        $data=[
            'appeal_time'=>time(),
            'reason_for_appeal'=>$post['reason_for_appeal'],
            'statement_description'=>$post['statement_description'],
            'picture_of_appeal'=>$post['picture_of_appeal'],
            'q_appeal_status'=>1,

        ];
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update($data);
        if($Order){
            return  json(['code'=>200,'msg'=>'已申诉','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'申诉失败，请稍后再试','data'=>'']);
        }
    }



    /**
     * 开锁订单
     * Ordertaker/order/unlocking_order
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function  unlocking_order(Request $request)
    {
        $post=$request->post();
        $store=\app\api\model\Order::where(['id'=>$post['order_id']])
//            ->field('id,actual_price,trailer_address,trailer_longitude,trailer_latitude,user_location,user_longitude,user_latitude,user_name,user_phone,rider_status,on_site_photos,defective_vehicle_model,createtime')
            ->find();
        $data['actual_price']=$store['actual_price'];
        $data['expected_arrival']='00:00';
        $data['rider_status']=$store['rider_status'];
        $data['aftersale_status']=$store['aftersale_status'];
        $data['q_appeal_status']=$store['q_appeal_status'];
        $data['vehicle']=[
            'text'=>'锁',
//            'trailer_address'=>$store['trailer_address'],
//            'trailer_longitude'=>$store['trailer_longitude'],
//            'trailer_latitude'=>$store['trailer_latitude'],
//            'km'=>getDistance($post['lat'],$post['lng'],$store['trailer_latitude'],$store['trailer_longitude'])
        ];
        $data['Destination']=[
            'text'=>'目',
            'user_location'=>$store['user_location'],
            'user_longitude'=>$store['user_longitude'],
            'user_latitude'=>$store['user_latitude'],
            'km'=>getDistance($post['lat'],$post['lng'],$store['user_latitude'],$store['user_longitude']),
            'consignee'=>$store['consignee'],
            'mobile'=>$store['mobile'],
        ];
        //现场照片
        $data['on_site_photos']=$store['on_site_photos'];
        //开锁类型
        $data['unlock_type']=$store['unlock_type'];
        $data['time']=[
            //下单时间
            'createtime'=>date('Y-m-d H:i:s',$store['createtime']),
            //接单时间
            'order_acceptance_time'=>date('Y-m-d H:i:s',$store['order_acceptance_time']),
            //到达目的地时间
            'arrival_time_at_destination'=>date('Y-m-d H:i:s',$store['arrival_time_at_destination']),
            //服务完成时间
            'service_completion_time'=>date('Y-m-d H:i:s',$store['service_completion_time']),
        ];
        $data['refund']=[
            //退款原因
            'refund_content'=>$store['refund_content'],
            //退款时间
            'refund_apply_time'=>$store['refund_apply_time'],
            //退款描述
            'describe_credentials'=>$store['describe_credentials'],
        ];

        $data['appeal']=[
            //申诉时间
            'appeal_time'=>$store['appeal_time'],
            //申诉原因
            'reason_for_appeal'=>$store['reason_for_appeal'],
            //申述描述
            'statement_description'=>$store['statement_description'],
            //申述图片
            'picture_of_appeal'=>$store['picture_of_appeal'],
        ];
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
    }


    /**
     * 开锁点击接单
     * Ordertaker/order/unlocking_orders
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function unlocking_orders(Request $request)
    {
        $post=$request->post();
        $rider_status=\app\api\model\Order::where(['id'=>$post['order_id']])->value('rider_status');
        if($rider_status==1){
            return  json(['code'=>404,'msg'=>'订单已被接取，请选择其他的订单','data'=>'']);
        }
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>1,'order_acceptance_time'=>time(),'q_user_id'=>$this->order_taker_id]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已接单','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'接单失败，请稍后再试','data'=>'']);
        }
    }
    /**
     * 开锁已到达目的地
     * Ordertaker/order/unlocking_arriving_at_the_destination
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function unlocking_arriving_at_the_destination(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>3,'arrival_time_at_destination'=>time()]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已到达车辆位置','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'确认状态失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 开锁确定服务完成
     * Ordertaker/order/service_completed
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function unlocking_service_completed(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>4,'service_completion_time'=>time()]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已到达车辆位置','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'确认状态失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 开锁删除订单
     * Ordertaker/order/unlocking_delete_order
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function unlocking_delete_order(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->delete();
        if($Order){
            return  json(['code'=>200,'msg'=>'订单已删除','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'删除失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 开锁接单员申诉
     * Ordertaker/order/complaint_from_the_order_taker
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function unlocking_complaint_from_the_order_taker(Request $request)
    {
        $post=$request->post();
        $data=[
            'appeal_time'=>time(),
            'reason_for_appeal'=>$post['reason_for_appeal'],
            'statement_description'=>$post['statement_description'],
            'picture_of_appeal'=>$post['picture_of_appeal'],
            'q_appeal_status'=>$post['q_appeal_status'],

        ];
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update($data);
        if($Order){
            return  json(['code'=>200,'msg'=>'已申诉','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'申诉失败，请稍后再试','data'=>'']);
        }
    }


    /**
     * 服务订单
     * Ordertaker/order/service_order
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public  function  service_order(Request $request)
    {
        $post=$request->post();
        $store=\app\api\model\Order::where(['id'=>$post['order_id']])->find();
        $data['actual_price']=$store['actual_price'];
        $data['expected_arrival']='00:00';
        $data['rider_status']=$store['rider_status'];
        $data['aftersale_status']=$store['aftersale_status'];
        $data['q_appeal_status']=$store['q_appeal_status'];
        $data['vehicle']=[
            'text'=>'服',
//            'trailer_address'=>$store['trailer_address'],
//            'trailer_longitude'=>$store['trailer_longitude'],
//            'trailer_latitude'=>$store['trailer_latitude'],
//            'km'=>getDistance($post['lat'],$post['lng'],$store['trailer_latitude'],$store['trailer_longitude'])
        ];
        $data['Destination']=[
            'text'=>'目',
            'user_location'=>$store['user_location'],
            'user_longitude'=>$store['user_longitude'],
            'user_latitude'=>$store['user_latitude'],
            'km'=>getDistance($post['lat'],$post['lng'],$store['user_latitude'],$store['user_longitude']),
            'consignee'=>$store['consignee'],
            'mobile'=>$store['mobile'],
        ];
        //现场照片
        $data['on_site_photos']=$store['on_site_photos'];
        //开锁类型
        $data['unlock_type']=$store['unlock_type'];
        $data['time']=[
            //下单时间
            'createtime'=>date('Y-m-d H:i:s',$store['createtime']),
            //接单时间
            'order_acceptance_time'=>date('Y-m-d H:i:s',$store['order_acceptance_time']),
            //到达目的地时间
            'arrival_time_at_destination'=>date('Y-m-d H:i:s',$store['arrival_time_at_destination']),
            //服务完成时间
            'service_completion_time'=>date('Y-m-d H:i:s',$store['service_completion_time']),
        ];
        $data['refund']=[
            //退款原因
            'refund_content'=>$store['refund_content'],
            //退款时间
            'refund_apply_time'=>$store['refund_apply_time'],
            //退款描述
            'describe_credentials'=>$store['describe_credentials'],
        ];

        $data['appeal']=[
            //申诉时间
            'appeal_time'=>$store['appeal_time'],
            //申诉原因
            'reason_for_appeal'=>$store['reason_for_appeal'],
            //申述描述
            'statement_description'=>$store['statement_description'],
            //申述图片
            'picture_of_appeal'=>$store['picture_of_appeal'],
        ];
        return  json(['code'=>200,'msg'=>'请求成功','data'=>$data]);
    }

    /**
     * 服务点击接单
     * Ordertaker/order/service_orders
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function service_orders(Request $request)
    {
        $post=$request->post();
        $rider_status=\app\api\model\Order::where(['id'=>$post['order_id']])->value('rider_status');
        if($rider_status==1){
            return  json(['code'=>404,'msg'=>'订单已被接取，请选择其他的订单','data'=>'']);
        }
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>1,'order_acceptance_time'=>time(),'q_user_id'=>$this->order_taker_id]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已接单','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'接单失败，请稍后再试','data'=>'']);
        }
    }
    /**
     * 服务已到达目的地
     * Ordertaker/order/service_arriving_at_the_destination
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function service_arriving_at_the_destination(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>3,'arrival_time_at_destination'=>time()]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已到达车辆位置','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'确认状态失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 服务确定服务完成
     * Ordertaker/order/service_completed
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function service_service_completed(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update(['rider_status'=>4,'service_completion_time'=>time()]);
        if($Order){
            return  json(['code'=>200,'msg'=>'已到达车辆位置','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'确认状态失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 服务删除订单
     * Ordertaker/order/service_delete_order
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function service_delete_order(Request $request)
    {
        $post=$request->post();

        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->delete();
        if($Order){
            return  json(['code'=>200,'msg'=>'订单已删除','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'删除失败，请稍后再试','data'=>'']);
        }
    }

    /**
     * 服务接单员申诉
     * Ordertaker/order/service_complaint_from_the_order_taker
     * @param Request $request
     * @return \think\response\Json|void
     */
    public  function service_complaint_from_the_order_taker(Request $request)
    {
        $post=$request->post();
        $data=[
            'appeal_time'=>time(),
            'reason_for_appeal'=>$post['reason_for_appeal'],
            'statement_description'=>$post['statement_description'],
            'picture_of_appeal'=>$post['picture_of_appeal'],
            'q_appeal_status'=>$post['q_appeal_status'],

        ];
        $Order=\app\api\model\Order::where(['id'=>$post['order_id']])->update($data);
        if($Order){
            return  json(['code'=>200,'msg'=>'已申诉','data'=>'']);
        }else{
            return  json(['code'=>404,'msg'=>'申诉失败，请稍后再试','data'=>'']);
        }
    }

}